#include "CSkyBox.h"
using namespace Content;
using namespace Util;
CSkyBox::CSkyBox()
{
  textureFile = "";
  vertices = 0;
  indices = 0;
  numVertices = 24;
  numTriangles = 6*2;
  numIndices = numTriangles*3;
  initVertices();
  initIndices();
  pD3DDevice = Engine::CDevice::getInstance()->getD3DDevice();
  pSkyCubeTexture = 0;
}

CSkyBox::~CSkyBox()
{

}

void CSkyBox::loadCubeTexture(std::string texture_in)
{
  if (pSkyCubeTexture)
  {
    SAFE_RELEASE(pSkyCubeTexture);
  }

  HRESULT result=D3DXCreateCubeTextureFromFile( pD3DDevice, texture_in.c_str() ,&pSkyCubeTexture );
  if ( result != S_OK)
  {
    LOG_INFO("Cube Texture nem letezik:" << texture_in)
    pSkyCubeTexture = 0;
    textureFile = "";
  }
  else
  {
    textureFile =texture_in;
  }
}

void CSkyBox::clean()
{
  SAFE_RELEASE(pSkyCubeTexture);
  pD3DDevice = 0;
}

IDirect3DCubeTexture9* CSkyBox::getCubeTexture()
{
  return pSkyCubeTexture;
}

void CSkyBox::initVertices()
{
  if (0 ==vertices)
  {
      vertices = new PrimitiveVertex[numVertices]();
  }
  float a = 1000.0f;
  float b = 1000.0f;
  float c = 1000.0f;


  PrimitiveVertex v1 = {-a/2.0f, -c/2.0f, -b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v2 = {a/2.0f, -c/2.0f, -b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v3 = {a/2.0f, -c/2.0f, b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v4 = {-a/2.0f, -c/2.0f, b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v5 = {-a/2.0f, c/2.0f, -b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v6 = {a/2.0f, c/2.0f, -b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v7 = {a/2.0f, c/2.0f, b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v8 = {-a/2.0f, c/2.0f, b/2.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};

  float normX,normY,normZ;
  
  for (int face = 0; face < 6; ++face)
  {
    switch (face)
    {
      case 0:
        vertices[face*4]=v1;
	      vertices[face*4+1]=v2;
	      vertices[face*4+2]=v4;
	      vertices[face*4+3]=v3;

        normX = 0.0f;
        normY = -1.0f;
        normZ = 0.0f;
        break;
      case 1:

        vertices[face*4]=v2;
	      vertices[face*4+1]=v6;
	      vertices[face*4+2]=v3;
	      vertices[face*4+3]=v7;

        normX = -1.0f;
        normY = 0.0f;
        normZ = 0.0f;
        break;
      case 2:

        vertices[face*4]=v4;
	      vertices[face*4+1]=v3;
	      vertices[face*4+2]=v8;
	      vertices[face*4+3]=v7;

        normX = 0.0f;
        normY = 0.0f;
        normZ = 1.0f;
        break;
      case 3:

        vertices[face*4]=v1;
	      vertices[face*4+1]=v4;
	      vertices[face*4+2]=v5;
	      vertices[face*4+3]=v8;

        normX = -1.0f;
        normY = 0.0f;
        normZ = 0.0f;
        break;
      case 4:

        vertices[face*4]=v1;
	      vertices[face*4+1]=v5;
	      vertices[face*4+2]=v2;
	      vertices[face*4+3]=v6;

        normX = 0.0f;
        normY = 0.0f;
        normZ = -1.0f;
        break;
      case 5:

        vertices[face*4]=v5;
	      vertices[face*4+1]=v8;
	      vertices[face*4+2]=v6;
	      vertices[face*4+3]=v7;

        normX = 0.0f;
        normY = 1.0f;
        normZ = 0.0f;
        break;
    }


	  vertices[face*4].nX=normX;
	  vertices[face*4].nY=normY;
	  vertices[face*4].nZ=normZ;
	  vertices[face*4].tX=0.0f;
	  vertices[face*4].tY=0.0f;



	  vertices[face*4+1].nX=normX;
	  vertices[face*4+1].nY=normY;
	  vertices[face*4+1].nZ=normZ;
	  vertices[face*4+1].tX=0.0f;
	  vertices[face*4+1].tY=0.0f;

	  vertices[face*4+2].nX=normX;
	  vertices[face*4+2].nY=normY;
	  vertices[face*4+2].nZ=normZ;
	  vertices[face*4+2].tX=0.0f;
	  vertices[face*4+2].tY=0.0f;

	  vertices[face*4+3].nX=normX;
	  vertices[face*4+3].nY=normY;
	  vertices[face*4+3].nZ=normZ;
	  vertices[face*4+3].tX=0.0f;
	  vertices[face*4+3].tY=0.0f;
  }


}

void CSkyBox::initIndices()
{
    if (0 == indices)
  {
      indices = new WORD[numIndices]();
  }
  for (int face = 0; face <6; ++face)
  {
    indices[face*6] = face*4 + 0;
    indices[face*6+1] = face*4 + 2;
    indices[face*6+2] = face*4 + 1;

    indices[face*6+3] = face*4 + 2;
    indices[face*6+4] = face*4 + 3;
    indices[face*6+5] = face*4 + 1;
  }
}

void CSkyBox::onLostDevice()
{
  LOG_INFO("Sky on lost")
  if (pSkyCubeTexture)
  {
    pSkyCubeTexture->Release();
    pSkyCubeTexture = 0;
  }
}

void CSkyBox::onResetDevice()
{
  loadCubeTexture(textureFile);
}